package com.uzai.console.web.wechatwork;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.dto.GroupCreateTaskDTO;
import com.uzai.console.dto.GroupCreateTaskQueryDTO;
import com.uzai.console.dto.GroupCreateTaskRecordQueryDTO;
import com.uzai.console.entity.GroupCreateTask;
import com.uzai.console.service.wwgroup.GroupCreateTaskRecordService;
import com.uzai.console.service.wwgroup.GroupCreateTaskService;
import com.uzai.console.vo.GroupCreateTaskRecordVO;
import com.uzai.console.vo.GroupCreateTaskVO;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 *
 */
@Api(value = "企业微信自动建群", tags = "企业微信|自动建群")
@ApiSort(6)
@RestController
@Slf4j
public class WxWorkGroupCreateController extends BaseController {

    @Autowired
    private GroupCreateTaskService groupCreateTaskService;
    @Autowired
    private GroupCreateTaskRecordService groupCreateTaskRecordService;

    @ApiOperation(value = "新增企微群自动创建任务", notes = "新增企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/add")
    public Object add(@RequestBody GroupCreateTaskDTO param) {
        if (Objects.isNull(param) || StringUtils.isAnyBlank(param.getName(), param.getGroupName())
                || ObjectUtils.isEmpty(param.getDeviceUniqueIds()) || ObjectUtils.isEmpty(param.getGroupMembers())
                || ObjectUtils.anyNull(param.getGroupNum(), param.getIntervalLeft(), param.getIntervalRight())) {
            throw new BusinessException("新增自动创建任务参数错误");
        }
        if (param.getDeviceUniqueIds().size() > 10 || param.getGroupNum() > 100) {
            throw new BusinessException("机器人最多支持10个/群数量最多支持100个");
        }
        GroupCreateTask groupCreateTask = new GroupCreateTask();
        BeanUtils.copyProperties(param, groupCreateTask);
        groupCreateTask.setId(IdUtil.getSnowflakeNextId());
        groupCreateTask.setDeviceType(DeviceTypeEnum.DT_ANDROID_WORK_WECHAT.getId());
        groupCreateTask.setDeviceUniqueId(JSON.toJSONString(param.getDeviceUniqueIds()));
        groupCreateTask.setGroupMembers(JSON.toJSONString(param.getGroupMembers()));
        groupCreateTask.setStatus(1);
        groupCreateTask.setCreateTime((int) (System.currentTimeMillis() / 1000L));
        groupCreateTask.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        return groupCreateTaskService.add(groupCreateTask);
    }

    @ApiOperation(value = "修改企微群自动创建任务", notes = "修改企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/edit")
    public Object edit(@RequestBody GroupCreateTaskDTO param) {
        if (Objects.isNull(param) || StringUtils.isAnyBlank(param.getName(), param.getGroupName())
                || ObjectUtils.isEmpty(param.getDeviceUniqueIds()) || ObjectUtils.isEmpty(param.getGroupMembers())
                || ObjectUtils.anyNull(param.getId(), param.getGroupNum(), param.getIntervalLeft(), param.getIntervalRight())) {
            throw new BusinessException("修改自动创建任务参数错误");
        }
        if (param.getDeviceUniqueIds().size() > 10 || param.getGroupNum() > 100) {
            throw new BusinessException("机器人最多支持10个/群数量最多支持100个");
        }
        return groupCreateTaskService.edit(param);
    }

    @ApiOperation(value = "搜索企微群自动创建任务", notes = "搜索企微群自动创建任务", response = GroupCreateTaskVO.class)
    @PostMapping("/wwGroupCreateTask/search")
    public Object search(@RequestBody GroupCreateTaskQueryDTO query) {
        query.setDeviceType(DeviceTypeEnum.DT_ANDROID_WORK_WECHAT.getId());
        return groupCreateTaskService.search(query);
    }


    @ApiOperation(value = "获取企微群自动创建任务", notes = "获取企微群自动创建任务", response = GroupCreateTaskVO.class)
    @PostMapping("/wwGroupCreateTask/get")
    public Object get(@RequestBody GroupCreateTaskQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("获取详情参数错误");
        }
        GroupCreateTask byId = groupCreateTaskService.getById(query.getId());
        GroupCreateTaskVO groupCreateTaskVO = new GroupCreateTaskVO();
        BeanUtils.copyProperties(byId, groupCreateTaskVO);
        groupCreateTaskVO.setDeviceUniqueIds(JSON.parseArray(byId.getDeviceUniqueId(), Long.class));
        groupCreateTaskVO.setGroupMembers(JSON.parseArray(byId.getGroupMembers(), String.class));
        return groupCreateTaskVO;
    }

    @ApiOperation(value = "删除企微群自动创建任务", notes = "删除企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/delete")
    public Object delete(@RequestBody GroupCreateTaskQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("删除详情参数错误");
        }
        groupCreateTaskService.delete(query.getId());
        return true;
    }

    @ApiOperation(value = "开启企微群自动创建任务", notes = "开启企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/open")
    public Object open(@RequestBody GroupCreateTaskQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("开启详情参数错误");
        }
        groupCreateTaskService.open(query.getMerId(), query.getId(), DeviceTypeEnum.DT_ANDROID_WORK_WECHAT.getId());
        return true;
    }

    @ApiOperation(value = "暂停企微群自动创建任务", notes = "暂停企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/pause")
    public Object pause(@RequestBody GroupCreateTaskQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("暂停详情参数错误");
        }
        groupCreateTaskService.pause(query.getId());
        return true;
    }

    @ApiOperation(value = "结束企微群自动创建任务", notes = "结束企微群自动创建任务", response = Boolean.class)
    @PostMapping("/wwGroupCreateTask/end")
    public Object end(@RequestBody GroupCreateTaskQueryDTO query) {
        if (Objects.isNull(query) || Objects.isNull(query.getId())) {
            throw new BusinessException("暂停详情参数错误");
        }
        groupCreateTaskService.end(query.getId());
        return true;
    }

    @ApiOperation(value = "搜索企微群自动创建任务记录", notes = "搜索企微群自动创建任务记录", response = GroupCreateTaskRecordVO.class)
    @PostMapping("/wwGroupCreateTaskRecord/search")
    public Object record(@RequestBody GroupCreateTaskRecordQueryDTO query) {
        return groupCreateTaskRecordService.search(query);
    }

    @ApiOperation(value = "删除企微群自动创建任务记录", notes = "删除企微群自动创建任务记录", response = Boolean.class)
    @PostMapping("/wwGroupCreateTaskRecord/delete")
    public Object recordDelete(@RequestBody GroupCreateTaskRecordQueryDTO query) {
        return groupCreateTaskRecordService.delete(query.getId());
    }
}
